package yf.mall.controller.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUserId;
import com.yf.response.R;
import com.yf.response.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yf.mall.common.Const;
import yf.mall.entity.CommentEntity;
import yf.mall.entity.MallGoodsEntity;
import yf.mall.entity.MaterialEntity;
import yf.mall.entity.ShopEntity;
import yf.mall.enums.MallGoodsShelfEnum;
import yf.mall.enums.MaterialStatusEnum;
import yf.mall.service.CommentService;
import yf.mall.service.MallGoodsService;
import yf.mall.service.MaterialService;
import yf.mall.service.ShopService;
import yf.mall.vo.MaterialVo;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/material")
public class ApiMaterialController {
    @Autowired
    private MaterialService materialService;
    @Autowired
    private ShopService shopService;
    @Autowired
    private MallGoodsService mallGoodsService;

    /** que
     * 列表爆款素材
     */
    @RequestMapping("/list")
    public R list(Long shopId,String goodsName,int page,int limit) {
        PageHelper.startPage(page, limit);
        List<MaterialEntity> list = materialService.apiFindAll(shopId,goodsName);
        return R.ok(list);
    }

    /** que
     * 上传素材
     */
    @RequestMapping("/save")
    public R save(@LoginUserId Long userId, @RequestBody MaterialEntity materialEntity) {

        materialEntity.setUserId(userId); //发表用户id
        materialEntity.setCreateTime(new Date()); // 发布时间待
        materialEntity.setStatus(MaterialStatusEnum.AUDIT.getIndex());  //状态待审核
        MaterialVo vo = new MaterialVo();
        vo.setMaterial(materialEntity);

        materialService.saveVo(vo);
        return R.ok(vo.getMaterial());
    }

    /**
     * 获取所有已通过的店铺
     * @return
     */
    @RequestMapping("/findShop")
    public R findShop(){
        List<ShopEntity> shopEntityList = shopService.findAll();
        return R.ok(shopEntityList);
    }

    /**
     * 获取店铺所有上架的产品
     * @param shopId
     * @return
     */
    @RequestMapping("/findByUserId/{shopId}")
    public R findByShopId(@PathVariable("shopId") Long shopId) {
        Long userId = null;
        if (shopId != null && !shopId.equals(Const.ADMIN_ID)){
            ShopEntity shopEntity = shopService.get(shopId);
            userId = shopEntity.getUserId();
        }
        List<MallGoodsEntity> mallGoodsEntity = mallGoodsService.findByUserId(userId, MallGoodsShelfEnum.ON.getIndex());
        return R.ok(mallGoodsEntity);
    }

    /**
     * 修改素材
     */
    @RequestMapping("/update")
    public R update(@LoginUserId Long userId,@RequestBody MaterialEntity material){
        if (!material.getUserId().equals(userId)){
            throw new RRException("权限不足");
        }
        material.setStatus(MaterialStatusEnum.AUDIT.getIndex());
        materialService.update(material);
        return R.ok(material);
    }

    /**
     * 根据用户id获取素材
     */
    @RequestMapping("/findMaterial")
    public R findMaterial(@LoginUserId Long userId,int page,int limit,String goodsName){
        PageHelper.startPage(page,limit);
        List<MaterialEntity> entities = materialService.findByUserIdOrGoodsName(userId,goodsName);
        if (entities != null && entities.size() > 0){
            for (MaterialEntity materialEntity : entities) {
                ShopEntity shopEntity = shopService.get(materialEntity.getShopId());
                //获取店铺
                if (shopEntity != null){
                    materialEntity.setShopEntity(shopEntity);
                }else {
                    ShopEntity shop = new ShopEntity();
                    shop.setId(Const.ADMIN_ID);
                    shop.setName("社集优选自营店");
                    shop.setAvatar("1603263922406WechatIMG110.png");
                    materialEntity.setShopEntity(shop);
                }
                //找出对应的产品
                MallGoodsEntity goodsEntity = mallGoodsService.get(materialEntity.getGoodsId());
                materialEntity.setGoodsImg(goodsEntity.getImg());
                materialEntity.setGoodsPrice(goodsEntity.getNowPrice());
            }
        }
        return R.ok(entities);
    }

}
